<template>
  <div>
    <Card>
      <Tables ref="tables" searchable search-place="top" :loading="listLoading" :columns="columns" v-model="list" >
        <template slot="handleButton">
          <ButtonGroup>
          <Button type="primary" @click="handleAdd">新增</Button>
          </ButtonGroup>
        </template>
        <template slot="searchButton">
          <div style="float:right">
            <Input placeholder="输入关键字搜索" class="search-input" v-model="listQuery.username">
              <Button @click="getList" slot="append" type="info" icon="ios-search"></Button>
            </Input>
          </div>
        </template>
        <template slot="action" slot-scope="props" style="width: 20px">
          <Button  type="primary" @click="handleUpdate(props)" size="small">编辑</Button>
          <Divider type="vertical" />
          <Button type="primary" @click="handleAuthority(props)" size="small">权限分配</Button>
          <Divider type="vertical" />
          <Poptip confirm title="永久删除数据?" transfer @on-ok="handleDelete(props)">
            <Button type="warning" size="small">删除</Button>
          </Poptip>
        </template>
      </Tables>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: left;">
          <Page v-show="!listLoading" :total="total" :page-size-opts="[10,20,50,100,500]" :current="listQuery.page" show-total show-sizer :page-size="listQuery.limit" @on-change="handleCurrentChange" @on-page-size-change="handleSizeChange"></Page>
        </div>
      </div>
    </Card>
    <Modal :title="textMap[dialogStatus]" v-model="dialogFormVisible" :mask-closable='false' :width="500">
      <Form ref="form" :model="form" :label-width="100" :rules="rules">
        <FormItem label="角色名称" prop="name">
          <Input v-model="form.name"/>
        </FormItem>
        <FormItem label="角色描述" prop="description">
          <Input type="textarea" v-model="form.description"/>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancel('form')">取消</Button>
        <Button type="primary" v-if="dialogStatus=='create'" @click="create('form')">确 定</Button>
        <Button type="primary" v-else @click="update('form')">确 定</Button>
      </div>
    </Modal>
    <Modal title="权限分配" v-model="dialogAuthorityVisible" :mask-closable='false' width="55">
      <role-authority :roleId="currentId" @closeAuthorityDialog="closeAuthorityDialog" ref="roleAuthority"></role-authority>
      <div slot="footer"></div>
    </Modal>
  </div>
</template>
<script>
import Tables from '_c/tables'
import { mapGetters } from 'vuex'
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj
} from '@/api/sys/role'
import roleAuthority from './components/roleAuthority'
export default {
  name: 'role',
  components: {
    Tables,
    'role-authority': roleAuthority
  },
  data () {
    return {
      form: {
        username: undefined,
        name: undefined,
        password: undefined
      },
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 20,
            message: '长度在 2 到 20 个字符',
            trigger: 'blur'
          }
        ]
      },
      dialogStatus: '',
      columns: [
        { type: 'selection', width: 60 },
        { title: '角色名称', key: 'name' },
        { title: '角色描述', key: 'description' },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          render: (h, params) => {
            return h(
              'div',
              this.$refs.tables.$scopedSlots.action(params)
            )
          }
        }
      ],
      list: [],
      listQuery: {
        page: 1,
        limit: 10,
        name: undefined,
        username: undefined
      },
      listLoading: false,
      total: 0,
      currentId: -1,
      dialogFormVisible: false,
      dialogAuthorityVisible: false
    }
  },
  created () {
    this.getList()
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    resetTemp () {
      this.form = {
        username: undefined,
        name: undefined,
        password: undefined
      }
    },
    handleUpdate (props) {
      getObj(props.row.id)
        .then(response => {
          this.form = response.data
          this.dialogFormVisible = true
          this.dialogStatus = 'update'
        })
    },
    getList (params) {
      this.listLoading = true
      page(this.listQuery)
        .then(res => {
          this.list = res.data.rows
          this.total = res.data.total
          this.listLoading = false
        })
    },
    create (formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form)
            .then(() => {
              this.dialogFormVisible = false
              this.getList()
              this.$Notice.success({
                title: '成功',
                desc: '添加成功'
              })
            })
        } else {
          return false
        }
      })
    },
    update (formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          this.form.password = undefined
          putObj(this.form.id, this.form).then(() => {
            this.dialogFormVisible = false
            this.getList()
            this.$Notice.success({
              title: '成功',
              desc: '修改成功'
            })
          })
        } else {
          return false
        }
      })
    },
    cancel (formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    handleAdd () {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleAuthority (props) {
      const roleId = props.row.id
      this.dialogAuthorityVisible = true
      this.currentId = roleId
      if (this.$refs.roleAuthority !== undefined) {
        this.$refs.roleAuthority.getList()
      }
    },
    handleCurrentChange (val) {
      this.listQuery.page = val
      this.getList()
    },
    handleSizeChange (val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleDelete (params) {
      delObj(params.row.id)
        .then(() => {
          this.$Notice.success({
            title: '成功',
            desc: '删除成功'
          })
          this.getList()
        })
    },
    closeAuthorityDialog () {
      this.dialogAuthorityVisible = false
    }
  }
}
</script>
